package fxana;

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.TickUnit;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;

public class FxPlot implements Runnable {
	JFreeChart Chart=null;
	int index=0;
	public FxPlot(){

		DateAxis dateaxis=new DateAxis("Date");	
		Chart = new JFreeChart("Fxana Chart",new XYPlot(null,
					 																		dateaxis,
					 																		new NumberAxis("value"),
					 																		null));
	}
	
	public void SetPlotTitle(String title){
		Chart.setTitle(title);
	}
	
	public void SetPlot(XYDataset dataset,XYItemRenderer renderer){
	
		XYPlot plot=Chart.getXYPlot();
		plot.setDataset(index,dataset);
		plot.setRenderer(index, renderer);
		index++;
	}
		
	
	public void SetPlotAxisRangeY(double d, double e){
		XYPlot plot=Chart.getXYPlot();
		ValueAxis valueAxis = plot.getRangeAxis();
		if (valueAxis==null){
			return;
		}
		valueAxis.setAutoRange(false);
		valueAxis.setRange(d, e);
		valueAxis.setVisible(true);
	}
	
	public void SetPlotAxisRangeX(long start , long end){
		XYPlot plot=Chart.getXYPlot();
		ValueAxis valueAxis = plot.getDomainAxis();		
		valueAxis.setAutoRange(false);
		valueAxis.setRange(start,end);
		valueAxis.setVisible(true);
	}
	
	public void SetPlotTickunitY(double tick){
		XYPlot plot=Chart.getXYPlot();
		ValueAxis valueAxis = plot.getRangeAxis();
		TickUnits tickUnits = new TickUnits();
		TickUnit unit = new NumberTickUnit(tick);
		tickUnits.add(unit);		
		valueAxis.setStandardTickUnits(tickUnits);
	}
	
	public void SetPlotTickunitX(int dayunit){
		XYPlot plot=Chart.getXYPlot();
		ValueAxis valueAxis = plot.getDomainAxis();
		if (valueAxis==null){
			return;
		}
		TickUnits tickUnits = new TickUnits();
		TickUnit unit=new DateTickUnit(DateTickUnitType.DAY, dayunit);
		tickUnits.add(unit);		
		valueAxis.setStandardTickUnits(tickUnits);
	}
	
	public void SavePlotAsPNG(String plotfilename,int xsize,int ysize){
		System.err.println("Plot Start..."+" "+"File: "+plotfilename+" "+"Size: "+xsize+"x"+ysize);
			
		try{
			File OutPutFile=new File(plotfilename);
			ChartUtilities.saveChartAsPNG(OutPutFile, Chart, xsize, ysize);			
		}catch(IOException e){
			System.err.println("Plot faild!\n");
		}
		System.err.println("Plot finished!");	
	}
	
	public void Show(){
		ChartFrame frame=new ChartFrame("Fxana Chart",Chart);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static CandlestickRenderer GetCandlestkRendterer(){
		return new CandlestickRenderer();
	}
	
	public static StandardXYItemRenderer GetStandardXYItemRenderer(){
		return new StandardXYItemRenderer();	
	}
	
	public static BarRenderer GetBarRenderer(){
		BarRenderer renderer=new BarRenderer();
		renderer.setItemMargin(0.30);
        renderer.setDrawBarOutline(false);
		return renderer;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		this.Show();		
	}


}
